<?php
date_default_timezone_set("PRC");
//error_reporting(0);

/**
 * Created by PhpStorm.
 * User: lovel
 * Date: 2017/10/22
 * Time: 23:09
 */
class Add_student_model extends MY_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function add_student($data)
    {
        $data = json_decode($data);
        if ($data === null) {
            $name = $data['name'];
            $sex = $data['sex'];
            $exec = $data['exec'];
            $school = $data['school'];
            $grade = $data['grade'];
            $class = $data['class'];
            $pos = $data['pos']; //文件列表
            $phone = $data['phone']; //家长电话
            $course = $data['course']; //报名课程
            $plan = $data['plan']; //分配班级
            $late_mark = $data['late_mark']; //文件列表
        } else {
            $name = $data->name;
            $sex = $data->sex;
            $exec = $data->exec;
            $school = $data->school;
            $grade = $data->grade;
            $class = $data->class;
            $pos = $data->pos;
            $phone = $data->phone;
            $course = $data->course; //报名课程
            $plan = $data->plan; //分配班级            $late_mark = '';
        }

        $pass = substr($phone, -6);
        $last_date = date("Y.m.d");
//        $sql = /** @lang text */
//            "INSERT INTO ws_baseinfo(name,last_date,sex,exec,school,grade,class,pos,late_mark,phone,pass) VALUE('$name','$last_date',$sex,$exec,$school,$grade,$class,$pos,'$late_mark',$phone,$pass)";
        $this->db->trans_begin();//回滚事物
        $array = array(
            'name' => $name,
            'sex' => $sex,
            'exec' => $exec,
            'school' => $school,
            'grade' => $grade,
            'class' => $class,
            'pos' => $pos,
            'phone' => $phone,
            'pass' => $pass,
            'last_date' => $last_date,
            'late_mark' => ''
        );
        $query = $this->my_insert('ws_baseinfo', $array);
        $array = array(
            'studentId' => $query,
            'classId' => $plan[1],
            'gradeId' => $plan[0],
            'tenantId' => '1'
        );

//        var_dump($array);
        switch ($course) {
            case 1:
                $query = $this->my_insert('ws_class_yuwen', $array);
                break;
            case 2:
                $query = $this->my_insert('ws_class_shuxue', $array);
                break;
            case 3:
                $query = $this->my_insert('ws_class_yingyu', $array);
                break;
            case 4:
                $query = $this->my_insert('ws_class_shufa', $array);
//                var_dump($query);
                break;
            case 5:
                $query = $this->my_insert('ws_class_aoshu', $array);
                break;
            case 6:
                $query = $this->my_insert('ws_class_zuowen', $array);
                break;
            case 7:
                $query = $this->my_insert('ws_class_wantuo', $array);
                break;
        };

        $this->db->trans_complete();
        $error = $this->db->error();
//        var_dump($this->db->trans_status());
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return json_encode(array('result' => ERROR, 'msg' => $this->config->item('mysql_msg')[$error['code']], 'code' => $error['code']));
        } else {
            $this->db->trans_commit();
        }

        if ($query) {
            return json_encode(array('result' => SUCCESS, 'msg' => $this->config->item('mysql_msg')[$error['code']]));
        }
        return json_encode(array('result' => ERROR, 'msg' => $this->config->item('mysql_msg')[$error['code']], 'code' => $error['code']));
    }
}